﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LicPdaService
{
    //成品库存查询
    public class PDAXmlInproductProcess
    {
        public static string GetPDAInproductData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string master_label = string.Empty;
            string sku = string.Empty;
            string qty = string.Empty; //良品数量
            string allocated_qty = string.Empty;
            string picked_qty = string.Empty;
            string backup_qty = string.Empty; //备货数量
            string check_qty = string.Empty; //待检数量
            string putaway_qty = string.Empty; //待上架数量 
            string rejects_qty = string.Empty; //不良品数量
            string serial = string.Empty;
            string return_status = "OK";
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            try
            {
                #region 参数源
                //App.ws = new Fcl.WSOP.sqldbop();
                //App.ws.Init(System.Configuration.ConfigurationManager.ConnectionStrings["adpwmsConnectionString"].ConnectionString);
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("product");

                try
                {
                    userid = nodeSku.SelectSingleNode("userid").InnerText;
                    whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                    serial = nodeSku.SelectSingleNode("serial").InnerText;


                    #region 数据源获取
                    //获取对应sku
                    strSql = "SELECT sku FROM ReceiptDetail WHERE udf1='" + serial + "'";
                    DataTable dtsku = App.GetDataTable(App.DBconWMS, strSql, "ReceiptDetail");
                    if (dtsku.Rows.Count > 0)
                    {
                        sku = dtsku.Rows[0]["sku"].ToString();
                    }
                    else
                    {
                        sku = "sku获取失败";
                    }

                    //良品数量获取
                    strSql = "SELECT isnull(sum(qty)-sum(picked_qty)-sum(allocated_qty),0) as qty FROM Inventory AS I,ReceiptDetail AS R ";
                    strSql += "    WHERE I.loc=R.loc AND I.lot=R.lot AND I.lpn=R.lpn AND R.udf1='" + serial + "' AND I.status='OK'";
                    DataTable dt = App.GetDataTable(App.DBconWMS,strSql, "Inventory");
                    if (dt.Rows.Count > 0)
                    {
                        qty = dt.Rows[0]["qty"].ToString();
                    }
                    else
                    {
                        return_status = "良品数据获取失败";
                    }

                    //已拣数量 获取
                    strSql = "  SELECT isnull(sum(picked_qty),0) as picked_qty FROM Inventory AS I,ReceiptDetail AS R ";
                    strSql += "    WHERE I.loc=R.loc AND I.lot=R.lot AND I.lpn=R.lpn AND R.udf1='" + serial + "' AND I.status='OK'";
                    DataTable dtpick = App.GetDataTable(App.DBconWMS, strSql, "Inventor");
                    if (dtpick.Rows.Count > 0)
                    {
                        picked_qty = dtpick.Rows[0]["picked_qty"].ToString();
                    }
                    else
                    {
                        return_status = "已拣数量库存数据获取失败";
                    }

                    //不良品数量 获取
                    strSql = "  SELECT isnull(sum(qty),0) as rejects_qty FROM Inventory AS I,ReceiptDetail AS R ";
                    strSql += "  WHERE I.loc=R.loc AND I.lot=R.lot AND I.lpn=R.lpn AND R.udf1='" + serial + "' AND I.status='MRB'";
                    DataTable dtrejects = App.GetDataTable(App.DBconWMS, strSql, "Invento");
                    if (dtrejects.Rows.Count > 0)
                    {
                        rejects_qty = dtrejects.Rows[0]["rejects_qty"].ToString();
                    }
                    else
                    {
                        return_status = "不良品库存数据获取失败";
                    }
                    #endregion

                }
                catch(Exception ex) { 
                 App.Log("PDAXmlInproductProcess.cs-2", ex.Message.ToString());
                }
                #endregion

                #region 构建XML报文
                XmlNode product_result = docreturn.CreateElement("product_return");

                XmlElement useridResult = docreturn.CreateElement("userid");
                useridResult.InnerText = userid;
                product_result.AppendChild(useridResult);

                XmlElement whseidResult = docreturn.CreateElement("whseid");
                whseidResult.InnerText = whseid;
                product_result.AppendChild(whseidResult);

                XmlElement serialResult = docreturn.CreateElement("serial");
                serialResult.InnerText = serial;
                product_result.AppendChild(serialResult);

                XmlElement skuResult = docreturn.CreateElement("sku");
                skuResult.InnerText = sku;
                product_result.AppendChild(skuResult);

                XmlElement qtyResult = docreturn.CreateElement("qty");
                qtyResult.InnerText = qty;
                product_result.AppendChild(qtyResult);

                XmlElement picked_qtyResult = docreturn.CreateElement("picking_qty");
                picked_qtyResult.InnerText = picked_qty;
                product_result.AppendChild(picked_qtyResult);

                XmlElement rejects_qtyResult = docreturn.CreateElement("rejects_qty");
                rejects_qtyResult.InnerText = rejects_qty;
                product_result.AppendChild(rejects_qtyResult);

                XmlElement return_statusResult = docreturn.CreateElement("return_status");
                return_statusResult.InnerText = return_status;
                product_result.AppendChild(return_statusResult);

                rootResult.AppendChild(product_result);
                #endregion
            }
            catch (Exception ex)
            {
                App.Log("PDAXmlInproductProcess.cs-1", ex.Message.ToString());
                //throw new Exception("异常源：PDAXmlInventoryProcess:GetPDAInventoryData;" + "  " + ex.Message);
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;

        }
    }
}
